home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / DMTDEMOS / GETFAT.PAS < prev    next >
Pascal/Delphi Source File  |  1994-07-02  |  6KB  |  218 lines

  1. program GetFATsample;
  2.  
  3. (*
  4.  
  5.   The File Allocation Table (FAT) is an array used by DOS to keep track of
  6.   which clusters on a disk have been allocated for each file or directory.
  7.   As a program creates a new file or adds to an existing one, DOS allocates
  8.   sectors for that file, writes the data to a given sectors, and keeps track
  9.   of the allocated sectors by recording them in the FAT.
  10.  
  11.   Depending on the number of clusters on the disk, the FAT consists of an array
  12.   of either 12-bit or 16-bit entries. Disks with more than 4086 clusters have a
  13.   16-bit FAT; those with 4086 or less clusters have a 12-bit FAT.
  14.  
  15.   This program reads the FAT sectors for the drive specified and then gets
  16.   information about allocated clusters, bad clusters, etc..
  17.  
  18. *)
  19.  
  20.  uses crt, DMT;
  21.  
  22.  var
  23.    LogDrvs          : ListOfDrvs;        { The ListOfDrvs data type is defined in the DMT unit }
  24.  
  25.    BPBbuffer        : DevParamsStruct;  { The DevParamsStruct data type is predefined in the DMT unit }
  26.  
  27.    FATbuffer,
  28.    BufferAddr       : pointer;
  29.  
  30.    BadClusters,
  31.    FreeClusters,
  32.    FileClusters,
  33.    ResClusters,
  34.    Head,
  35.    Track,
  36.    FirstSect,
  37.    StartFATsector,
  38.    SectCounter,
  39.    N               : word;
  40.  
  41.    DrvLetter       : char;
  42.  
  43.    DrvsCount,
  44.    Index           : byte;
  45.  
  46. procedure DecFAT12( BufSeg, BufOfs  : word  );
  47.  
  48. (* Decodes a FAT of 12-bit entries *)
  49.  
  50.  var
  51.    Cluster,
  52.    Offset,
  53.    FATentry,
  54.    TotalOfClusts : word;
  55.  
  56. begin
  57.   (* Determines the number of clusters or FAT entries on disk *)
  58.  
  59.   TotalOfClusts := Clusters( DrvLetter );
  60.  
  61.   (* Go through all of the FAT entries *)
  62.  
  63.   for Cluster := 2 to TotalOfClusts + 1 do
  64.     begin
  65.  
  66.       { Begin decode 12-bit FAT entries }
  67.  
  68.       Offset := ( Cluster * 3 ) div 2;
  69.       FATentry := memw[ BufSeg : BufOfs + Offset ];
  70.  
  71.       if ( ( Cluster mod 2 ) = 0 ) then
  72.         FATentry := ( FATentry and $0FFF )
  73.       else
  74.         FATentry := ( FATentry shr 4 );
  75.  
  76.       { End decode 12-bit FAT entries }
  77.  
  78.       case ( FATentry ) of
  79.  
  80.         (* Possible FAT Entry values *)
  81.  
  82.                $000 : inc( FreeClusters );
  83.  
  84.          $FF0..$FF6 : inc( ResClusters );
  85.  
  86.                $FF7 : inc( BadClusters );
  87.  
  88.          $FF8..$FFF : inc( FileClusters );
  89.  
  90.       end;
  91.     end;
  92. end;
  93.  
  94. procedure DecFAT16( BufSeg, BufOfs : word );
  95.  
  96. (* Decodes a FAT of 16-bit entries *)
  97.  
  98.  var
  99.    Cluster,
  100.    Offset,
  101.    FATEntry,
  102.    TotalOfClusts : word;
  103.  
  104. begin
  105.   Offset := 0;
  106.  
  107.   (* Determines the number of clusters or FAT entries on disk *)
  108.  
  109.   TotalOfClusts := Clusters( DrvLetter );
  110.  
  111.   (* Go through all of the FAT entries *)
  112.  
  113.   for Cluster := 2 to TotalOfClusts + 1 do
  114.     begin
  115.       Offset := Cluster * 2;
  116.  
  117.       FATentry := memw[ BufSeg : BufOfs + Offset ];
  118.  
  119.       case ( FATentry ) of
  120.  
  121.        (* Possible FAT Entry values *)
  122.  
  123.                $0000 : inc( FreeClusters );
  124.  
  125.         $FFF0..$FFF6 : inc( ResClusters );
  126.  
  127.                $FFF7 : inc( BadClusters );
  128.  
  129.         $FFF8..$FFFF : inc( FileClusters );
  130.  
  131.       end;
  132.     end;
  133. end;
  134.  
  135. begin
  136.   BadClusters := 0;
  137.   FreeClusters := 0;
  138.   FileClusters := 0;
  139.   ResClusters := 0;
  140.   N := 0;
  141.  
  142.   Color( 7, 0 );
  143.   clrscr;
  144.  
  145.   GetDrvs( LogDrvs, DrvsCount );   { Call GetDrvs procedure }
  146.  
  147.   write( #13#10, 'Select Drive ( ');
  148.  
  149.   for Index := 1 to DrvsCount do
  150.     write( LogDrvs[ Index ], ': ' );
  151.  
  152.   write( ') [ :]' );
  153.  
  154.   gotoxy( wherex - 3, wherey );
  155.   DrvLetter := upcase( readkey );
  156.   writeln( DrvLetter );
  157.  
  158.   GetBPB( DrvLetter, BPBbuffer );  { Call the GetBPB procedure }
  159.  
  160.   if ( ErrFlag ) then
  161.     begin
  162.       writeln( #7 );
  163.       writeln( ShowError( GetErrCode ) );
  164.     end
  165.   else
  166.     with BPBbuffer do
  167.       begin
  168.         (* Allocates memory for FAT sectors buffer *)
  169.  
  170.         getmem( FATbuffer, SectsPerFAT * 512 );
  171.  
  172.         (* Determines the start of the FAT sectors *)
  173.  
  174.         StartFATsector := HiddenSects + BootSects;
  175.  
  176.         (* Reads the FAT sectors from disk and place it into a buffer *)
  177.  
  178.         for SectCounter := StartFATSector to ( StartFATSector + SectsPerFAT - 1 ) do
  179.           begin
  180.  
  181.             (*
  182.               Translates a logical FAT sector number into a physical sector
  183.               consisting of head, track, and sector numbers.
  184.             *)
  185.  
  186.              Head      := ( SectCounter div SectsPerTrack ) mod NumOfHeads;
  187.              Track     := SectCounter div ( SectsPerTrack * NumOfHeads );
  188.              FirstSect := SectCounter mod SectsPerTrack;
  189.  
  190.              BufferAddr := ptr( seg( FATbuffer^ ), ofs( FATbuffer^ ) + n );
  191.  
  192.              (* Reads a FAT sector from disk and place it into a buffer *)
  193.  
  194.              ReadSect( DrvLetter, Head, Track, FirstSect, 1, BufferAddr );
  195.  
  196.              inc( n, BytesPerSect );
  197.           end;
  198.  
  199.         (* Determines the type of FAT used by the disk *)
  200.  
  201.         if ( FATtype( DrvLetter ) = 16 ) then
  202.           DecFAT16( seg( FATbuffer^ ), ofs( FATbuffer^ ) )
  203.         else
  204.           DecFAT12( seg( FATbuffer^ ), ofs( FATbuffer^ ) );
  205.  
  206.         clrscr;
  207.         writeln;
  208.         writeln( 'Disk Drive specified        : ', DrvLetter );
  209.         writeln( 'Number of free clusters     : ', InsComma( FreeClusters ) );
  210.         writeln( 'Number of user files        : ', InsComma( FileClusters ) );
  211.         writeln( 'Number of bad clusters      : ', InsComma( BadClusters ) );
  212.         writeln( 'Number of reserved clusters : ', InsComma( ResClusters ) );
  213.       end;
  214.   GetEnter;
  215. end.
  216.  
  217.  
  218.